package com.tedu.egweibo1.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.tedu.egweibo1.mapper.WeiboMapper;
import com.tedu.egweibo1.pojo.dto.WeiboDTO;
import com.tedu.egweibo1.pojo.entity.User;
import com.tedu.egweibo1.pojo.entity.Weibo;
import com.tedu.egweibo1.result.JsonResult;
import com.tedu.egweibo1.result.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;


@Api(tags = "微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博功能")
    @PostMapping("/insert")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setUserId(user.getId());
            weibo.setCreated(new Date());
            weiboMapper.InsertWeibo(weibo);
            return JsonResult.success();


        }
        return new JsonResult(StatusCode.NOT_LOGIN);

    }


    @ApiOperation("微博首页列表")
    @GetMapping("/selectIndex")
    public JsonResult selectWeiboIndex() {
        return JsonResult.success(weiboMapper.SelectWeiboIndex());

    }

    @ApiOperation("微博详情页")
    @GetMapping("/selectById")
    @ApiImplicitParam(name="id",value = "微博ID",required = true,example = "1")
    public JsonResult selectWeiboById(int id) {
        return JsonResult.success(weiboMapper.SelectWeiboDetail(id));

    }



}
